/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/


/**
 * @brief  Recursive-Descent-Function of non-terminal directDeclarator.
 *
 * @author  WXQ#XDU
 * @date    2024.12
 */

#include "../_parser_inner.h"

#define THIS_NT_ID  directDeclarator

// directDeclarator            //------------------ LL(1) production 6
//   : ID
//     (
//        ( '[' (INT_LITERAL) ? ']' )   // optional length for dim-1
//        ( '['  INT_LITERAL    ']' )*
//     )?
//
t_tree_ptr RDF_directDeclarator (t_parser_ptr pThis) {
    int productionId = PRD_ID_UNKNOWN;
    t_tree_ptr root = tree_new_parent(THIS_NT, productionId);
    enter_nonterminal ( pThis, THIS_NT, root );
    CHECK_NULLTREE_ABORT ( root, root );
// -------  BEGIN of local action

    productionId = 6;

    // consume varName or arrayName
    t_tree_ptr subTree = parser_match(pThis, TK_ID);
    CHECK_NULLTREE_ABORT ( subTree, root );
    tree_add_child(root, subTree);

    int dimCounter = 0;
    while (TK_LBRACKET == LOOK_AHEAD(pThis)) {     // ('[' INT ']')*
        ++ dimCounter;
        subTree = parser_match(pThis, TK_LBRACKET);  // consume '['
        CHECK_NULLTREE_ABORT (subTree, root);
        tree_add_child(root, subTree);

        //
        // NOTICE: If the length of first dimension is omitted,
        //         the symbol-table-builder will correct it!
        //

        if ( TK_RBRACKET != LOOK_AHEAD(pThis) ) {  // not '[', maybe int number
            subTree = parser_match(pThis, TK_INT_LITERAL);
            CHECK_NULLTREE_ABORT (subTree, root);
            tree_add_child(root, subTree);
        } else if ( dimCounter > 1 ) {
            // report error ONLY to continue analysis
            t_position_ptr pos = token_get_position(NEXT_TOKEN(pThis));
            ERROR_REPORT_INPUT(pos->filePath, pos->line, pos->column,
                               EC_DIMSZ_MISS, dimCounter);
        }

        subTree = parser_match(pThis, TK_RBRACKET); // consume ']'
        CHECK_NULLTREE_ABORT (subTree, root);
        tree_add_child(root, subTree);
    } // end of while-loop for "[ expr ][ expr ]...[ expr ]"


// -------  END of local action
    if (PRD_ID_UNKNOWN == productionId) {
        ERROR_REPORT_BUG("Forgot to correct the value of "
                         "productionId in RDF_" NT_TEXT "()");
    }
    tree_reset_production( root, productionId );

LABEL_EXIT_RDF:
    exit_nonterminal  ( pThis, THIS_NT, root );
    return root;
}

